package com.erp.framework.util.poi;

import com.erp.framework.config.GlobalConfig;
import org.apache.poi.hssf.usermodel.HSSFPrintSetup;
import org.apache.poi.ss.usermodel.*;

import java.io.File;

/**
 * @ClassName: POIUtil
 * @Author: bo
 * @Description:
 * @Date: 2021/1/20 17:14
 * @Version: 1.0
 */
public class POIUtil {

    public static final String FONT_NAME = "宋体";
    public static final short FONT_NORMAL_SIZE = 200;
    public static final short FONT_TITLE_SIZE = 280;

    /**
     * 获取下载路径
     *
     * @param filename 文件名称
     */
    public static String getDownloadPath(String filename) {
        String downloadPath = GlobalConfig.getDownloadPath() + filename;
        File desc = new File(downloadPath);
        if (!desc.getParentFile().exists()) {
            desc.getParentFile().mkdirs();
        }
        return downloadPath;
    }

    /**
     * 获取下载路径
     *
     * @param filename 文件名称
     */
    public static String getTemplatePath(String filename) {
        String templatePath = GlobalConfig.getTemplatePath() + filename;
        File desc = new File(templatePath);
        if (!desc.getParentFile().exists()) {
            desc.getParentFile().mkdirs();
        }
        return templatePath;
    }



    public static CellStyle normalFontStyle(Workbook workbook) {
        return getFontStyle(workbook, false, FONT_NORMAL_SIZE);
    }

    public static CellStyle boldFontStyle(Workbook workbook) {
        return getFontStyle(workbook, true, FONT_NORMAL_SIZE);
    }

    public static CellStyle titleFontStyle(Workbook workbook) {
        return getFontStyle(workbook, true, FONT_TITLE_SIZE);
    }


    public static CellStyle getFontStyle(Workbook workbook, boolean bold, short size) {
        CellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setFont(getFont(workbook, bold, size));
        return cellStyle;
    }

    public static Font getFont(Workbook workbook, boolean bold, short size) {
        Font font = workbook.createFont();
        font.setBold(bold);
        font.setFontName(FONT_NAME);
        font.setFontHeight(size);
        return font;
    }


    public static CellStyle underLineStyle(Workbook workbook, boolean bold) {
        CellStyle underLine = workbook.createCellStyle();
        underLine.setBorderBottom(BorderStyle.THIN);
        underLine.setFont(getFont(workbook, bold, FONT_NORMAL_SIZE));
        return underLine;
    }


    /**
     * 设置页眉
     *
     * @param header
     */
    public static void doHeader(Header header) {

    }

    /**
     * 设置页脚
     *
     * @param footer
     */
    public static void doFooter(Footer footer) {

    }

    /**
     * 打印设置
     *
     * @param printSetup
     */
    public static void doPrintSetup(PrintSetup printSetup) {
        // 纸张大小
        printSetup.setPaperSize(HSSFPrintSetup.A4_PAPERSIZE);
        // 缩放 默认 100 无缩放
//        printSetup.setScale((short) 100);
        // 打印方向 默认 纵向
//        printSetup.setLandscape(false);
        // 打印起始页码 默认 0
//        printSetup.setPageStart((short) 0);
        // 设置页高
//        printSetup.setFitHeight((short) 1);
        // 设置页宽
//        printSetup.setFitHeight((short) 0);
        // 设置页眉边距
//        printSetup.setHeaderMargin(2.0);
        // 设置页脚边距
//        printSetup.setFooterMargin(1.0);

    }


    public static void doUnderLine(Row row, CellStyle style, int cellNum) {
        for (int i = 0; i <= cellNum; i++) {
            Cell cell = row.createCell(i);
            cell.setCellStyle(style);
        }
    }


    public static String getValue(Object obj) {
        return obj == null ? "" : obj.toString();
    }

}
